package com.dragon.rabbitmq.service;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.dragon.loan.entity.AcLoanApplyInfo;
import com.dragon.loan.repository.AcLoanApplyInfoRepository;
import com.dragon.loan.service.LoanInfoIDragonService;
import com.marvels.common.utils.MarvelsLogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**ac_loan 表状态变更服务
 * @author hejie
 */
@Service("acLoanStatusListener")
public class AcLoanStatusListener {
	@Autowired
    private LoanInfoIDragonService loanInfoIDragonService ;
	@Autowired
	private AcLoanApplyInfoRepository acLoanApplyInfoRepository;

	public void onMessage(String msgJson){
		try{
			MarvelsLogUtil.getInstance().info("ac_loan 表状态变更服务");
            MarvelsLogUtil.getInstance().info("ac_loan_json:"+msgJson);
            JSONObject msgJObj = new JSONObject();
            try {
            	msgJObj = JSONObject.parseObject(msgJson);
			} catch (Exception e) {
	            //将 "{}"替换成{}
	            if (msgJson.contains("\"{")) {
	            	msgJson=msgJson.substring(1, msgJson.length());
	        	}
	            if (msgJson.contains("}\"")) {
	            	msgJson=msgJson.substring(0,msgJson.length()-1);
	        	}
	            if(msgJson.contains("\\")){
	            	msgJson=msgJson.replaceAll("\\\\", "");
	            }
	            msgJObj = JSONObject.parseObject(msgJson);
			}
            //loanId
            String loanId = msgJObj.getString("loanId");
            //更改后状态
            String status = msgJObj.getString("status");
            //更改前状态
            String originalState = msgJObj.getString("originalState");
            //资金源
            String fundCode = msgJObj.getString("fundCode");
            loanInfoIDragonService.updateLoanStatus(loanId, status);
            
            //紧急方案修改核定金额
            BigDecimal principalAmtAct = msgJObj.getBigDecimal("principalAmtAct");
            AcLoanApplyInfo loanApply = acLoanApplyInfoRepository.findByLoanId(loanId);
            BigDecimal loanMoneyAct = loanApply.getLoanMoneyAct();
            if(principalAmtAct!=null && principalAmtAct.compareTo(BigDecimal.ZERO)!=0 && principalAmtAct.compareTo(loanMoneyAct==null?BigDecimal.ZERO:loanMoneyAct)!=0) {
            	loanApply.setLoanMoneyAct(principalAmtAct);
            }
            MarvelsLogUtil.getInstance().info("[预备同步新核心的资金源] loanId = "+loanId+" , status = "+status+" , fundCode = "+fundCode);
            /*if((StringUtils.equals("S01", status) || StringUtils.equals("S00", status)) && !StringUtils.isEmpty(fundCode)){
            	Integer fundSource = Integer.valueOf(fundCode);
            	loanApply.setFundSource(fundSource);
            }*/
            if(!StringUtils.isEmpty(fundCode)){
            	Integer fundSource = Integer.valueOf(fundCode);
            	loanApply.setFundSource(fundSource);
            }
            acLoanApplyInfoRepository.updateByLoanId(loanApply);
        }catch(Exception e){
            MarvelsLogUtil.getInstance().error("ac_loan 表状态变更服务错误",e);
        }
	}

}
